package cn.train.controler;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.train.model.Ordertable;
import cn.train.service.OrderTableService;
import cn.train.util.Pageutil;


@WebServlet("/OrderTableServlet")
public class OrderTableServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String currPage = req.getParameter("pageIndex");
		if (currPage == null) {
			currPage = "1";
		}
		int pageIndex = Integer.parseInt(currPage);
		String name = req.getParameter("name");
		OrderTableService orderTableService = new OrderTableService();

		// 总条数
		int count = orderTableService.getCount();
		Pageutil pu = new Pageutil();
		// 总页数
		int totalPage = pu.getTotalPage(count, Pageutil.pageSize);
		// 判断第一页和最后一页
		if (pageIndex < 1) {
			pageIndex = 1;
		} else if (pageIndex > totalPage) {
			pageIndex = totalPage;
		}
		req.setAttribute("pageIndex", pageIndex);
		req.setAttribute("totalPage", totalPage);
		req.setAttribute("name", name);
		List<Ordertable> list = null;
		List<Ordertable> list1 = null;
		if (name != null) {
			list = orderTableService.getOrdertableByName(name, pageIndex - 1, Pageutil.pageSize);
		} else {
			list1 = orderTableService.getOrdertableByPage(pageIndex, Pageutil.pageSize);
		}

		req.setAttribute("list", list);
		req.setAttribute("list1", list1);

		req.getRequestDispatcher("back/admin/dingdan.jsp").forward(req, resp);

	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doGet(req, resp);
	}
}
